import Vue from 'vue'
import VueResource from 'vue-resource'
import { buildUrl } from '@/utils/index'

Vue.use(VueResource)
const Api = {
  databaseUrl: '/hebada/article/',
  oneArticle: '/hebada/catalog/',
  currArticle: '/hebada/',
  articleList: '/hebada/article/list/',
  catalogUrl: '/hebada/catalog/list',
  photoList: '/hebada/catalog/{{catalogId}}/photo/list',
  oneProduct: '/hebada/product/',
  productList: '/hebada/catalog/{{catalogId}}/product/list',
  recommend: '/hebada/recommend/list/'
}
export function fetchRecommendList (param) {
  var url = Api.recommend
  return Vue.http.post(url, param).then(function (res) {
    return res.body
  })
}
export function fetchRecommendPdocuctList (param) {
  var url = Api.productList
  url = buildUrl(url, {catalogId: param})
  return Vue.http.post(url, {'pageSize': 1}).then(function (res) {
    return res.body
  })
}

export function fetchPhotoList (param) {
  var url = Api.photoList
  url = buildUrl(url, {catalogId: param.params.catalogId})
  return Vue.http.post(url, {'pageNumber': param.pageNumber, 'pageSize': 6}).then(function (res) {
    return res.body
  })
}

export function fetchProductList (param) {
  var url = Api.productList
  url = buildUrl(url, {catalogId: param.params.catalogId})
  return Vue.http.post(url, {'pageNumber': param.pageNumber, 'pageSize': 6}).then(function (res) {
    return res.body
  })
}
export function fetchProduct (param) {
  var url = Api.oneProduct + param
  return Vue.http.get(url).then(function (res) {
    return res.body
  })
}
export function fetchArticleList (param) {
  var url = Api.articleList
  return Vue.http.post(url, param).then(function (res) {
    return res.body
  })
}

export function fetchArticle (param) {
  var url = Api.oneArticle + param + '/article'
  return Vue.http.get(url).then(function (res) {
    return res.body
  })
}
export function fetchCurrArticle (param) {
  var url = Api.currArticle + 'article/'+ param
  return Vue.http.get(url).then(function (res) {
    return res.body
  })
}
export function fetchCatalog () {
  var url = Api.catalogUrl
  return Vue.http.get(url).then(function (res) {
    return res.body
  })
}